<?php
/*
 * @package AJAX_Chat
 * @author Sebastian Tschan
 * @copyright (c) Sebastian Tschan
 * @license GNU Affero General Public License
 * @link https://blueimp.net/ajax/
 */

class CustomAJAXChat extends AJAXChat {

	// Returns an associative array containing userName, userID and userRole
	// Returns null if login is invalid
    // Rewrite this for Small World!!!
	function getValidLoginUserData() {
    
        if(! isset($_SESSION['user_id'])) {
            echo "Sorry...Please log into Study Buddies before accessing logs";
            exit;
        }
        $userData = array();
        $userData['userID'] = $_SESSION['user_id'];
        $tutor= $_SESSION['tutor'];

        if($tutor == 1)
        $userData['userName'] = $this->trimUserName("REACH_" . $_SESSION['user_first'] );

        else
        $userData['userName'] = $this->trimUserName($_SESSION['user_first'] . $_SESSION['user_last']);
        
        if($tutor == 1)
            $userData['userRole'] = AJAX_CHAT_ADMIN;
        else
            $userData['userRole'] = AJAX_CHAT_USER;
        return $userData;
    
  /*		
		$customUsers = $this->getCustomUsers();
		
		if($this->getRequestVar('password')) {
			// Check if we have a valid registered user:

			$userName = $this->getRequestVar('userName');
			$userName = $this->convertEncoding($userName, $this->getConfig('contentEncoding'), $this->getConfig('sourceEncoding'));

			$password = $this->getRequestVar('password');
			$password = $this->convertEncoding($password, $this->getConfig('contentEncoding'), $this->getConfig('sourceEncoding'));

			foreach($customUsers as $key=>$value) {
				if(($value['userName'] == $userName) && ($value['password'] == $password)) {
					$userData = array();
					$userData['userID'] = $key;
					$userData['userName'] = $this->trimUserName($value['userName']);
					$userData['userRole'] = $value['userRole'];
					return $userData;
				}
			}
			
			return null;
		}
        else {
			// Guest users:
			return $this->getGuestUser();
		}
    */
    
	}

	// Store the channels the current user has access to
	// Make sure channel names don't contain any whitespace
	function &getAllChannels() {

		if($this->_allChannels === null) {
            global $db;
			$this->_allChannels = array();
		
           /* //add network realm for network user currently has selected first
            $current_network_id = $_SESSION['current_network_id'];
            $current_network_name = $_SESSION['current_network_name'];
            $this->_allChannels[$current_network_name] = $current_network_id;
            */
            //populate course names and trim them
        
            $user_id = $_SESSION['user_id'];
            $sql = 'SELECT course_id from fb_user_courses WHERE user_id='.$user_id.';';
            $result = $db->Execute($sql);
            $defaultChannelFound = false;
                
         while(!$result->EOF) {  
            $curr_id= $result->fields['course_id'];
            $sql2 = 'SELECT course_dept,course_num from fb_courses WHERE id='.$curr_id.';';
            $result2 = $db->Execute($sql2);
            $course_name =  $result2->fields['course_dept'].'_'.$result2->fields['course_num'] ;
            $this->_allChannels[$course_name] = (int) $result->fields['course_id'];
            $result->MoveNext();
         }
            //Small World: return private realms the user is allowed to see
         /*   $sql = 'SELECT realm_id from user_private_realm WHERE user_id='.$_SESSION['user'].'';
            $result = $db->Execute($sql);
            
            while(!$result->EOF) {
                $realm_id = $result->fields['realm_id'];
                $sql2= 'SELECT realm_name from realms WHERE realm_id='.$realm_id.' and realm_type=0;';
                $result2=$db->Execute($sql2);
                $realmName = $this->getConfig('privateChannelPrefix'). $this->trimChannelName($result2->fields['realm_name']) .
                                $this->getConfig('privateChannelSuffix') ;
                $this->_allChannels[$realmName] = $realm_id;
                $result->MoveNext();
                
            }*/
          /*  if(!$defaultChannelFound) {
               $this->_allChannels = array_merge(
                    array(
                        //$this->trimChannelName($this->getConfig('defaultChannelName'))=>$this->getConfig('defaultChannelID')
                        //add network name as default realm
                        $this->trimChannelName($_SESSION['fb_network_name'])
                    ),
                    $this->_allChannels
                );
            }*/
            /*$this->_allChannels = array_merge(
                 array(
                     //add network name as default realm
                     $this->trimChannelName($_SESSION['current_network_name'] )=>(int)$_SESSION['current_network_id']
                 ),
                 $this->_allChannels
             );*/
       }
       return $this->_allChannels;

        

        /*	
			$customUsers = $this->getCustomUsers();
			
			// Get the channels, the user has access to:
			if($this->getUserRole() == AJAX_CHAT_GUEST) {
				$validChannels = $customUsers[0]['channels'];
			} else {
				$validChannels = $customUsers[$this->getUserID()]['channels'];
			}
			// Add the valid channels to the channel list (the defaultChannelID is always valid):
			foreach($this->getAllChannels() as $key=>$value) {
				// Check if we have to limit the available channels:
				if($this->getConfig('limitChannelList') && !in_array($value, $this->getConfig('limitChannelList'))) {
					continue;
				}
				
				if(in_array($value, $validChannels) || $value == $this->getConfig('defaultChannelID')) {
					$this->_channels[$key] = $value;
				}
			}
		}
		return $this->_channels;
		*/	
        
	}

	// Store all existing channels
	// Make sure channel names don't contain any whitespace
	function &getChannels() {
		if($this->_channels === null) {

            $this->_channels = array();
 
            $allChannels = $this->getAllChannels();
 
            foreach($allChannels as $key=>$value) {
                // Check if we have to limit the available channels:
              /*  if($this->getConfig('limitChannelList') && !in_array($value, $this->getConfig('limitChannelList'))) {
                    continue;
                }
                */
                // Add the valid channels to the channel list (the defaultChannelID is always valid):
                //TODO : Only add channels which the user has selected in his realm settings
                // FINISHED: we already check this in the previous function, just add the channels we've already selected
             //   if($value == $this->getConfig('defaultChannelID') || in_array($value, $this->getConfig('limitChannelList'))) {
                    $this->_channels[$key] = $value;
               // }
            }
        }
        return $this->_channels;
        
	/*		// Get all existing channels:
			$customChannels = $this->getCustomChannels();
			
			$defaultChannelFound = false;
			
			foreach($customChannels as $key=>$value) {
				$forumName = $this->trimChannelName($value);
				
				$this->_allChannels[$forumName] = $key;
				
				if($key == $this->getConfig('defaultChannelID')) {
					$defaultChannelFound = true;
				}
			}
			
			if(!$defaultChannelFound) {
				// Add the default channel as first array element to the channel list:
				$this->_allChannels = array_merge(
					array(
						$this->trimChannelName($this->getConfig('defaultChannelName'))=>$this->getConfig('defaultChannelID')
					),
					$this->_allChannels
				);
			}
		}
		return $this->_allChannels;
       */
	}

	function &getCustomUsers() {
		// List containing the registered chat users:
		$users = null;
		require(AJAX_CHAT_PATH.'lib/data/users.php');
		return $users;
	}
	
	function &getCustomChannels() {
		// List containing the custom channels:
		$channels = null;
		require(AJAX_CHAT_PATH.'lib/data/channels.php');
		return $channels;
	}

}
?>
